package com.pureut.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.production.domain.AmHrGuard;
import com.pureut.production.domain.AmHrGuardskills;
import com.pureut.production.domain.AmHrSkillrequest;
import com.pureut.production.domain.AmHrSkills;
import com.pureut.production.domain.vo.*;
import com.pureut.production.service.AmHrGuardService;
import com.pureut.production.service.AmHrSkillrequestService;
import com.pureut.production.service.AmHrSkillsService;
import com.pureut.system.api.RemoteUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/skillrequest")
public class AmHrSkillrequestController extends BaseController {
    @Resource
    private AmHrSkillrequestService amHrSkillrequestService;
    @Resource
    private AmHrSkillsService amHrSkillsService;

    @GetMapping("/list")
    public TableDataInfo list(AmHrSkillrequestVo amHrSkillrequestVo) {
        startPage();
        AmHrSkillrequest amHrSkillrequest = new AmHrSkillrequest();
        amHrSkillrequest.setSklCode(amHrSkillrequestVo.getSklCode());
        amHrSkillrequest.setEmpno(amHrSkillrequestVo.getEmpno());
        amHrSkillrequest.setEmpname(amHrSkillrequestVo.getEmpname());
        List<AmHrSkillrequest> list = amHrSkillrequestService.list(new QueryWrapper<>(amHrSkillrequest).orderByDesc("create_time"));
        TableDataInfo dataTable = getDataTable(list);
        List<AmHrSkillrequestRespVo> collect = list.stream().map(item -> {
            AmHrSkillrequestRespVo amHrGuardskillsRespVo = new AmHrSkillrequestRespVo();
            amHrGuardskillsRespVo.setId(item.getId());
            amHrGuardskillsRespVo.setEmpno(item.getEmpno());
            amHrGuardskillsRespVo.setEmpname(item.getEmpname());
            amHrGuardskillsRespVo.setSklCode(item.getSklCode());
            amHrGuardskillsRespVo.setPostCode(item.getPostCode());
            amHrGuardskillsRespVo.setPostName(item.getPostName());
            amHrGuardskillsRespVo.setSklLevel(item.getSklLevel());
            amHrGuardskillsRespVo.setReqempno(item.getReqempno());
            amHrGuardskillsRespVo.setReqempname(item.getReqempname());
            amHrGuardskillsRespVo.setReqdate(item.getReqdate());
            amHrGuardskillsRespVo.setCertifyempno(item.getCertifyempno());
            amHrGuardskillsRespVo.setCertifyempname(item.getCertifyempname());
            amHrGuardskillsRespVo.setCertifydate(item.getCertifydate());
            amHrGuardskillsRespVo.setCertifystatus(item.getCertifystatus());
            amHrGuardskillsRespVo.setRemark(item.getRemark());

            AmHrSkills sklCode = amHrSkillsService.getOne(new QueryWrapper<AmHrSkills>()
                    .eq("skl_code", item.getSklCode()));
            if (sklCode != null) {
                amHrGuardskillsRespVo.setSklName(sklCode.getSklName());
            }

            return amHrGuardskillsRespVo;
        }).collect(Collectors.toList());
        dataTable.setRows(collect);
        return dataTable;
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody AmHrSkillrequest amHrSkillrequest) {
        amHrSkillrequest.setReqempno(SecurityUtils.getUsername());
        amHrSkillrequest.setReqempname(SecurityUtils.getLoginUser().getSysUser().getNickName());
        amHrSkillrequest.setReqdate(new Date());
        amHrSkillrequest.setCreateTime(new Date());

        return toAjax(amHrSkillrequestService.save(amHrSkillrequest));
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(amHrSkillrequestService.removeById(id));
    }

    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) {
        return AjaxResult.success(amHrSkillrequestService.getById(id));
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody AmHrSkillrequest amHrSkillrequest) {
        return toAjax(amHrSkillrequestService.updateById(amHrSkillrequest));
    }

    @PostMapping("/cetify")
    public AjaxResult updatecetifyinfo(@Valid @RequestBody AmHrSkillrequest amHrSkillrequest) {
        if(ObjectUtils.isEmpty(amHrSkillrequest.getSklCode())){
            return AjaxResult.error("技能编码不可为空");
        }
        if(ObjectUtils.isEmpty(amHrSkillrequest.getEmpno()) || ObjectUtils.isEmpty(amHrSkillrequest.getEmpname())){
            return AjaxResult.error("员工工号和员工姓名不可为空");
        }
        if(ObjectUtils.isEmpty(amHrSkillrequest.getReqempno()) || ObjectUtils.isEmpty(amHrSkillrequest.getReqempname())){
            return AjaxResult.error("申请人工号和申请人姓名不可为空");
        }
        //点击认证按钮，更新认证人、认证日期、状态
        amHrSkillrequest.setCertifyempno(SecurityUtils.getUsername());
        amHrSkillrequest.setCertifyempname(SecurityUtils.getLoginUser().getSysUser().getNickName());
        amHrSkillrequest.setCertifydate(new Date());
        amHrSkillrequest.setCertifystatus("已认证");
        return AjaxResult.success(amHrSkillrequestService.updateById(amHrSkillrequest));
    }

    @PostMapping("/uncetify")
    @ApiOperation(value="认证",notes="参数")
    public AjaxResult cancelcetifyinfo(@Valid @RequestBody AmHrSkillrequest amHrSkillrequest) {
        if(ObjectUtils.isEmpty(amHrSkillrequest.getSklCode())){
            return AjaxResult.error("技能编码不可为空");
        }
        if(ObjectUtils.isEmpty(amHrSkillrequest.getEmpno()) || ObjectUtils.isEmpty(amHrSkillrequest.getEmpname())){
            return AjaxResult.error("员工工号和员工姓名不可为空");
        }
        if(ObjectUtils.isEmpty(amHrSkillrequest.getReqempno()) || ObjectUtils.isEmpty(amHrSkillrequest.getReqempname())){
            return AjaxResult.error("申请人工号和申请人姓名不可为空");
        }
        if(!amHrSkillrequest.getCertifystatus().equals("已认证")){
            return AjaxResult.error("当前申请未认证，不可取消认证");
        }
        //点击认证按钮，更新认证人、认证日期、状态
        amHrSkillrequest.setCertifyempno(null);
        amHrSkillrequest.setCertifyempname(null);
        amHrSkillrequest.setCertifydate(null);
        amHrSkillrequest.setCertifystatus("未认证");
        return toAjax(amHrSkillrequestService.lambdaUpdate()
                .set(AmHrSkillrequest::getCertifyempno,null)
                .set(AmHrSkillrequest::getCertifyempname,null)
                .set(AmHrSkillrequest::getCertifydate,null)
                .set(AmHrSkillrequest::getCertifystatus,"未认证")
                .eq(AmHrSkillrequest::getId,amHrSkillrequest.getId()).update());
    }
}
